﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using MvcApp.Models;
using Authority.Entity;
using Authority.Services;
using DoNet.Common.IoC;
using Microsoft.Practices.Unity;
using DoNet.FrameWork.Repository.Specification;

namespace MvcApp.CommonSupport
{
    public class CurrentUser
    {
        private readonly IUserService _userService;
        private readonly IModulesService _modulesService;
        private readonly IPermisionService _permisionService;
        //用户信息
        public User UserInfo { get; set; }

        //菜单权限
        public string MenuPermission { get; set; }

        //功能权限
        public Dictionary<string,string> ActionPermission { get; set; }

        public CurrentUser()
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                throw new Exception("用户未登录!");
            }
            if (HttpContext.Current.Session["CurrentUser"] == null)
            {
                //获取用户信息
                _userService = UnityContext.Current.Container.Resolve<IUserService>();
                ISpecification<User> condition = new DirectSpecification<User>(u => u.UserId == HttpContext.Current.User.Identity.Name);
                this.UserInfo = _userService.GetByCondition(condition);

                //获取菜单权限
                List<string> modulesCodes = new List<string>();
                _permisionService = UnityContext.Current.Container.Resolve<IPermisionService>();
                ActionPermission = new Dictionary<string, string>();
                List<UserControlsMap> ucms = _permisionService.GetUserPermision(this.UserInfo.Id);
                if (ucms.Count == 0)
                {
                    List<PostControlsMap> pcms = _permisionService.GetPostPermision(this.UserInfo.PostId);
                    foreach (var p in pcms)
                    {
                        ActionPermission.Add(p.ModulesCode.ToString(), p.PermisionTypes.ToString());
                        modulesCodes.Add(p.ModulesCode);
                    }
                }
                else
                {
                    foreach (var u in ucms)
                    {
                        ActionPermission.Add(u.ModulesCode.ToString(), u.PermisionTypes.ToString());
                        modulesCodes.Add(u.ModulesCode);
                    }
                }

                //获取菜单信息
                _modulesService = UnityContext.Current.Container.Resolve<IModulesService>();
                MenuData menuData = new MenuData(_modulesService, modulesCodes);
                this.MenuPermission = menuData.GetJson();

                HttpContext.Current.Session.Add("CurrentUser", this);
            }
            else
            {
                CurrentUser currentUser = (CurrentUser)HttpContext.Current.Session["CurrentUser"];
                this.UserInfo = currentUser.UserInfo;
                this.MenuPermission = currentUser.MenuPermission;
                this.ActionPermission = currentUser.ActionPermission;
            }
        }
    }
}
